package com.unidt.www.module.expert.service;

import com.unidt.www.framework.common.exception.ServiceException;
import com.unidt.www.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.unidt.www.framework.common.pojo.CommonResult;
import com.unidt.www.module.expert.domain.ExpertInvitationRuleDo;
import com.unidt.www.module.expert.domain.ExpertRuleUserDo;
import com.unidt.www.module.expert.form.ExpertConfirmForm;
import com.unidt.www.module.expert.mapper.ExpertInvitationRuleMapper;
import com.unidt.www.module.expert.mapper.ExpertRuleUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author sk
 * @Description:
 * @Date 2022/12/26 13:35
 **/
@Service
public class InvExpertConfirmServiceImpl implements IInvExpertConfirmService {

    @Autowired
    ExpertRuleUserMapper expertRuleUserMapper;

    @Autowired
    ExpertInvitationRuleMapper ruleMapper;

    @Override
    public CommonResult confirm(ExpertConfirmForm expertConfirmForm) {
        String code = expertConfirmForm.getCode();
        ExpertRuleUserDo expertRuleUserDo = expertRuleUserMapper.getByCode(code);
        if (Objects.isNull(expertRuleUserDo)) {
            throw new ServiceException(GlobalErrorCodeConstants.FAIL.getCode(), "邀请码不存在");
        }
        Integer status =  expertConfirmForm.getStatus();
        expertRuleUserDo.setConfirmTime(new Date());
        if(Integer.valueOf(1).equals(status)){
            /**
             * 查看是否已经过期间
             */
            Date expirationTime = expertRuleUserDo.getExpirationTime();
            if (new Date().after(expirationTime)) {
                throw new ServiceException(GlobalErrorCodeConstants.FAIL.getCode(), "邀请已过期");

            }
            /**
             * 验证邀请是否已满
             */
            Long ruleId = expertRuleUserDo.getRuleId();
            ExpertInvitationRuleDo ruleDo = ruleMapper.selectById(ruleId);
            /**
             * 邀约人数
             */
            Integer inviteesNumbers =  ruleDo.getInviteesNumbers();

            /**
             * 已确认邀约人数
             */
            List<ExpertRuleUserDo> ruleUserDos =  expertRuleUserMapper.getByRuleIdAndStatus(ruleId,1);

            Integer experts_count = ruleUserDos.size();

            if(inviteesNumbers>experts_count){

                expertRuleUserDo.setStatus(1);
                this.expertRuleUserMapper.updateById(expertRuleUserDo);
                return  CommonResult.success("确认成功过");
            }else {
                /**
                 * 名单已满足
                 */
                expertRuleUserDo.setStatus(3);
                this.expertRuleUserMapper.updateById(expertRuleUserDo);
                return  CommonResult.error(GlobalErrorCodeConstants.FAIL.getCode(),"确认失败,名额已满");

            }
        }else {
            /**
             * 拒绝
             */
            expertRuleUserDo.setStatus(-1);
            this.expertRuleUserMapper.updateById(expertRuleUserDo);
            return  CommonResult.success("确认成功");
        }

    }
}
